import React from 'react'
import {observable, action} from 'mobx';
import { Tools ,SearchBar,Icon} from 'weaver-mobile';
import { SearchAdvanced, ListAdvanced, RouteLayout, Browser, BrowserHrm, Upload, Reply } from 'weaver-mobile-page';
import * as API from '../../api/goal/goalDetail'
const getLabel = Tools.getLabel;

export default class GoalSubInfoStore {

    @observable loading = false;
    @observable subGoalList = []; //后台返回数据
    @observable datas = []; //前端展示数据 
    @observable reply = new Reply.Store()  // 没有 + 和 @ 功能，可以直接 new Reply.Store();

    /**
     * 获取子目标的数据
     */
    @action
    getSubGoalList = (params) =>{
        this.loading = true;
        this.clearData();
        API.getSubGoalList(params).then(action(res => {
           if(res.status){
                // this.subGoalList = res.subGoalList;
                this.initData(res.subGoalList)
           }else{

           }
           this.loading = false;
        }));
    }

    @action
    changeDatas = (datas) =>{
        this.datas = datas;
    }

    @action
    initData = (sublist) =>{
        sublist.forEach((sub, i) => {
            this.datas.push(this.formatSubGoalToData(sub, 1))
        })
    }

    @action
    clearData = () =>{
        this.subGoalList = [];
        this.datas = [];
    }

    onItemClick = (goalId, event) => {
        // const { changeTaskid, changeTaskDetail } = this.props.taskDetailStore
        event.preventDefault()
        event.stopPropagation()
        // this.props.history.replace("/taskHome/taskDetail/" + taskid)
        // changeTaskid(taskid)
        // changeTaskDetail({})
        // this.props.history.replace("/taskHome/taskDetail?id=" + taskid)
        window.openAppLink(null, goalId, "wrlgoal", null, null)
    }

    /**
     * 设置后端返回的数据格式
     */
    formatSubGoalToData = (data, deep) => {
        return {
            deep,
            haschild: data.childcount !== 0 && data.childcount !== "" && parseInt(data.childcount + "") > 0,
            isopen: false,
            key: data.key,
            name: <span className="title" style={{ width: window.innerWidth - (14 * deep + 24 + 65) }} onClick={(event) => this.onItemClick(data.key, event)}>{data.title}</span>,
            extra: <span className="right-info" onClick={(event) => this.onItemClick(data.key, event)}>{data.rightData.length > 0 ? data.rightData[0].name : ''}</span>
        }
    }



}